package gui;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import electro.Electrocardiograma;
import electro.Muestra;
import electro.Toma;

public class Graficador_Electro extends JPanel {

	private static final long serialVersionUID = 1L;
	private Electrocardiograma de;
	private int ini_width, ini_height;
	private int h_gap = 1;
		
	public Graficador_Electro(Electrocardiograma x){
		ini_width  = 700; 
		ini_height = 200;
		
		setBackground(Color.WHITE);  // Set background color.   See JComponent vs JPanel.
		setForeground(Color.black);  // Set the initial pen color. Defaults to black.
		setBorder(BorderFactory.createLineBorder(Color.black));
		de = x;
	}

	@Override     
	public Dimension getPreferredSize() {
	    return new Dimension(ini_width, ini_height);
	}
	
	public void dibuja(Graphics2D g){
		
		// int cm    = de.cant_muestras();  
		int x1    = 0;
		int i	  = 0;
		Muestra m, m_ant;
		
		// Linea horizontal (continua)
		g.drawLine(0, ini_height/2, ini_width, ini_height/2);

		// Lineas verticales (de a trazos)
		
		g.setColor(Color.gray);
		g.setStroke(new BasicStroke(1.0f,
									BasicStroke.CAP_BUTT,
									BasicStroke.JOIN_MITER,
									10.0f, new float[] { 10.0f, 10.0f }, 0.0f));
		
		
		while(x1<ini_width){
			g.drawLine(x1 , 0, x1, ini_width);		
			x1 += 25;
		}
		
		g.setColor(Color.black);
		g.setStroke(new BasicStroke());
		
		// Grafico los ptos del electro
		int media_altura = ini_height/2;
		
		int c_tomas = de.cant_tomas();
//		c_tomas = 1;
		for (int x=0;x<c_tomas;x++){
			
			Toma toma = de.da_toma(x);
			g.setColor(toma.getColor());
			m_ant = toma.da_muestra(0);
			m     = toma.da_muestra(1);
			x1    = 0;
					
			// Para cada muestra
			for(i=2;i<ini_width;i++){
				double medicion = toma(m_ant);
				double medicion1 = toma(m);
				g.drawLine(x1        , (int)Math.round(media_altura - (ini_height  * 0.35) * medicion),
						   x1 + h_gap, (int)Math.round(media_altura - (ini_height  * 0.35) * medicion1));
				x1 += h_gap;
				m_ant = m;
				m = toma.da_muestra(i);
			}			
			
		}
		

		

	}
	

	private double toma(Muestra m_ant) {
		int med = m_ant.getMedicion();
		double medicion = (double) med;
		medicion = medicion *.01 -1;
		return medicion;
	}	
	
	@Override
    public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		dibuja(g2);
	}

}
